package com.scm.factory;

import com.scm.common.enums.OrderProductInventStatus;
import com.scm.common.utils.AmountUtils;
import com.scm.common.utils.StringUtils;
import com.scm.dto.resp.invent.ScmInventoryProductStatDo;
import com.scm.dto.resp.order.OrderProductDo;
import com.scm.persist.model.ScmInventoryStashProduct;
import com.scm.persist.model.ScmOrderProduct;
import com.scm.service.IScmInventoryStashProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
@Slf4j
public class OrderProductFactory {

    @Autowired
    IScmInventoryStashProductService inventoryStashProductService;


    public List<OrderProductDo> createOrderProductDos(Long tenantId,List<ScmOrderProduct> orderProducts){

        List<OrderProductDo> list = Lists.newArrayList();

        if(CollectionUtils.isEmpty(orderProducts)){
            return list;
        }
        List<Long> productIds = Lists.newArrayList();
        for (ScmOrderProduct orderProduct : orderProducts) {
            if(orderProduct.getProductId()!=null&&orderProduct.getProductId()>0){
                productIds.add(orderProduct.getProductId());
            }
        }

        List<ScmInventoryStashProduct> productlist = inventoryStashProductService.queryListByProductIds(tenantId,productIds);

        Map<Long, ScmInventoryProductStatDo> productMap = new HashMap<>();
        if(org.apache.commons.collections4.CollectionUtils.isNotEmpty(productlist)){

            for (ScmInventoryStashProduct scmInventoryStashProduct : productlist) {

                ScmInventoryProductStatDo inventoryProductStatDo = productMap.get(scmInventoryStashProduct.getProductId());
                if(inventoryProductStatDo==null){
                    inventoryProductStatDo = new ScmInventoryProductStatDo();
                }
                inventoryProductStatDo.addScmInventoryStashProduct(scmInventoryStashProduct);

                productMap.put(scmInventoryStashProduct.getProductId(),inventoryProductStatDo);
            }
        }

        for (ScmOrderProduct orderProduct : orderProducts) {
            OrderProductDo orderProductDo = createOrderProductDo(orderProduct,productMap);
            list.add(orderProductDo);
        }

        return list;
    }

    public OrderProductDo createOrderProductDo(ScmOrderProduct orderProduct,Map<Long, ScmInventoryProductStatDo> productMap){

        OrderProductDo orderProductDo = new OrderProductDo();
        orderProductDo.setId(orderProduct.getId());
        orderProductDo.setTenantId(orderProduct.getTenantId());
        orderProductDo.setScmOrderId(orderProduct.getScmOrderId());
        orderProductDo.setProductCode(orderProduct.getProductCode());
        orderProductDo.setProductName(orderProduct.getProductName());
        orderProductDo.setProductSpec(orderProduct.getProductSpec());
        orderProductDo.setProductUnit(orderProduct.getProductUnit());
        orderProductDo.setProductPrice(orderProduct.getProductPrice());
        orderProductDo.setProductPriceStr(AmountUtils.fenToStr(orderProduct.getProductPrice()));
        orderProductDo.setAmount(orderProduct.getAmount());
        orderProductDo.setAmountStr(AmountUtils.fenToStr(orderProduct.getAmount()));
        orderProductDo.setProductCount(orderProduct.getProductCount());
        orderProductDo.setSendCount(orderProduct.getSendCount());
        orderProductDo.setProductId(orderProduct.getProductId());
        if(orderProduct.getProductId()!=null&&orderProduct.getProductId()>0){
            orderProductDo.setStatusDesc("已映射商品库数据");
            ScmInventoryProductStatDo inventoryProductStatDo = productMap.get(orderProduct.getProductId());
            if(inventoryProductStatDo!=null){
                if(inventoryProductStatDo.getStashCount()==1){
                    orderProductDo.setSingleStash(true);
                    orderProductDo.setStashName(inventoryProductStatDo.getStashProduct().get(0).getStashName());
                    orderProductDo.setStashId(inventoryProductStatDo.getStashProduct().get(0).getStashId());
                    orderProductDo.setInventoryStashProduct(inventoryProductStatDo.getStashProduct().get(0));
                }else{
                    orderProductDo.setSingleStash(false);
                }
                orderProductDo.setProductInventoryCount(inventoryProductStatDo.getAvailableCount());
                orderProductDo.setInventoryDesc(getInventoryDesc(inventoryProductStatDo,orderProductDo.getProductCount()));
            }
            else{
                orderProductDo.setInventoryDesc(OrderProductInventStatus.None.getName());
            }
        }else{
            orderProductDo.setStatusDesc("未映射到商品库数据，请同步");
            orderProductDo.setInventoryDesc(OrderProductInventStatus.None.getName());
        }

        return orderProductDo;

    }

    private String  getInventoryDesc(ScmInventoryProductStatDo inventoryProductStatDo,Integer productCount ){

        int productInventoryCount = inventoryProductStatDo.getAvailableCount();
        if(productCount==null){
            productCount=0;
        }
        if(productInventoryCount>=productCount){

            if(inventoryProductStatDo.getSingleStashAvailableCount()>=productCount){
                return OrderProductInventStatus.Ok.getName();
            }else{
                return OrderProductInventStatus.Ok2.getName();
            }
        }else{
            return OrderProductInventStatus.Not_Enough.getName();
        }
    }

    public static void main(String[] args) {


    }

}
